home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / gcctest / tests03.zoo / tstrl2.c < prev    next >
C/C++ Source or Header  |  1992-04-27  |  3KB  |  127 lines

  1. /*
  2.  * test strtol ++jrb and michal jaegermann
  3.  *
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include <errno.h>
  8. extern int errno;
  9.  
  10. #if __STDC__
  11. #include <stdlib.h>
  12. #include <limits.h>
  13. #else
  14. extern long strtol();
  15. #define const /* */
  16. #define LONG_MAX 2147483647L
  17. #define LONG_MIN (-2147483648L)
  18. #endif
  19.  
  20. #ifndef OK
  21. #define OK 0
  22. #endif
  23.  
  24. #ifndef EXIT_FAILURE
  25. #define EXIT_FAILURE 1
  26. #endif
  27.  
  28. #ifndef EXIT_SUCCESS
  29. #define EXIT_SUCCESS 0
  30. #endif
  31.  
  32. struct test {
  33.   const char    *string;    /* the string         */
  34.   long        val;        /* expected result     */
  35.   int        err;        /* expected errno     */
  36.   int        endp;        /* index of expected end ptr    */
  37. } tests [] = {
  38.   { "0",        0L,        OK,      1     },
  39.   { "-12345",        -12345L,    OK,      6     },
  40.   { "12345",        12345L,        OK,      5     },
  41.   { "-0x12345",        -0x12345L,    OK,      8     },
  42.   { "0x12345",        0x12345L,    OK,      7     },
  43.   { "-012345",        -012345L,    OK,      7     },
  44.   { "012345",        012345L,    OK,      6     },
  45.   { "10]",              10L,        OK,      2     },
  46.                              
  47.   { "2147483645L",    2147483645L,    OK,      10     },
  48.   { "2147483646L",    2147483646L,    OK,      10     },
  49.   { "2147483647L",    2147483647L,    OK,      10     },
  50.   { "2147483648L",    2147483647L,    ERANGE,  10     },
  51.   { "2147483649L",    2147483647L,    ERANGE,  10     },
  52.                              
  53.   { "-2147483646L",    -2147483646L,    OK,      11     },
  54.   { "-2147483647L",    -2147483647L,    OK,      11     },
  55.   { "-2147483648L",    -2147483648L,    OK,      11     },
  56.   { "-2147483649L",    -2147483648L,    ERANGE,  11     },
  57.   { "-2147484648L",    -2147483648L,    ERANGE,  11     },
  58.                              
  59.   { "       567L",    567L,        OK,      10     },
  60.   { "\t      567 ",    567L,        OK,      10     },
  61.   { "      -567]",     -567L,        OK,      10     },
  62.                              
  63.   { "ZZZZZZ",          0L,        OK,      0     },
  64.   { "0xGZZZZZ",          0L,        OK,      0     },
  65.   { "08ZZZZZZ",          0L,        OK,      1     },
  66.   { "+0x1ZZZZZZ",      1L,        OK,      4     },
  67.   { " -07188888",      -071L,        OK,      5     },
  68.                              
  69.   { " +017777777777+",    LONG_MAX,    OK,      14     },
  70.   { " +027777777777+",    LONG_MAX,    ERANGE,  14     },
  71.   { " -017777777777+",    LONG_MIN+1,    OK,      14     },
  72.   { " -020000000000+",    LONG_MIN,    OK,      14     },
  73.   { " -037777777777+",    LONG_MIN,    ERANGE,  14     },
  74.   { "-037777777777+",    LONG_MIN,    ERANGE,  13     },
  75.   { "037777777777+",    LONG_MAX,    ERANGE,  12     },
  76.                              
  77.   { ""            ,          0,       OK,      0     },
  78.   { "\t\t"        ,          0,       OK,      0     },
  79.   { "+"            ,          0,       OK,      0     },
  80.   { "-"            ,          0,       OK,      0     },
  81.   { NULL,               0,    OK,      0     }
  82. };
  83.  
  84. void fail(i, test, res, err, endp)
  85. int i;
  86. struct test *test;
  87. long res;
  88. int err;
  89. char *endp;
  90. {
  91.     printf("Test %d (\"%s\") failed\n", i, test->string);
  92.     printf("\tExpecting:\t%10ld\t%3d\t%x\n", test->val, test->err,
  93.  &(test->string[(test->endp)]));
  94.     printf("\tGot:\t\t%10ld\t%3d\t%x\n", res, err, endp);
  95. }
  96.  
  97. int main()
  98. {
  99.     int i, errs = 0;
  100.     long res;
  101.     char *endp;
  102.  
  103.     for(i = 0; tests[i].string; i++)
  104.     {
  105.     errno = OK;
  106.     res = strtol(tests[i].string, &endp, 0);
  107.     if(res != tests[i].val)
  108.     {
  109.        fail(i, &tests[i], res, errno, endp);
  110.        errs++;
  111.     }
  112.     else if(tests[i].err != errno)
  113.     {
  114.        fail(i, &tests[i], res, errno, endp);
  115.        errs++;
  116.     }
  117.     else if( &(tests[i].string[(tests[i].endp)]) != endp)
  118.     {
  119.        fail(i, &tests[i], res, errno, endp);
  120.        errs++;
  121.     }
  122.     }
  123.  
  124.     if (errs) printf("%d error(s)\n", errs);
  125.     return errs ?  EXIT_FAILURE : EXIT_SUCCESS;
  126. }
  127.